import {otherRouter, appRouter} from '../../router';
import Vue from 'vue'
const app = {
  state: {
    routers: [
      otherRouter,
      ...appRouter
    ],
    menuList: [],
    tagsList: [...otherRouter.children],
    pageOpenedList: [],
    currentPageName: '',
    currentPath: [
      {
        title: '首页',
        path: '',
        name: 'home_index'
      }
    ],  // 面包屑数组
    openedSubmenuArr: [],  // 要展开的菜单数组
    menuTheme: '', // 主题
    theme: '',
    currentWindowWidth: null,
    originRouters: null,
    webSocketMsg: null,
    cachePage: []
  },
  mutations: {
    getWebSocketMsg(state, wsInfo) {
      state.webSocketMsg = wsInfo
    },
    saveOriginRouter(state, routers) {
      state.originRouters = routers
    },
    getWindowWidth(state, width) {
      "use strict";
      state.currentWindowWidth = width
    },
    setTagsList(state, list) {
      state.tagsList.push(...list);
    },
    inCreateTag(state, tagObj) {
      state.pageOpenedList.splice(1, 0, tagObj);
    },
    removeTag(state, name) {
      state.pageOpenedList.map((item, index) => {
        if (item.name === name) {
          state.pageOpenedList.splice(index, 1);
        }
      });
    },
    moveToSecond(state, index) {
      let openedPage = state.pageOpenedList[index];
      state.pageOpenedList.splice(index, 1);
      state.pageOpenedList.splice(1, 0, openedPage);
      // 替换标签
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    setOpenedList(state) {
      state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]];
    },
    setCurrentPath(state, pathArr) {
      state.currentPath = pathArr;
    },
    setCurrentPageName(state, name) {
      state.currentPageName = name;
    },
    addOpenSubmenu(state, name) {
      let hasThisName = false;
      let isEmpty = false;
      if (name.length === 0) {
        isEmpty = true;
      }
      if (state.openedSubmenuArr.indexOf(name) > -1) {
        hasThisName = true;
      }
      if (!hasThisName && !isEmpty) {
        state.openedSubmenuArr.push(name);
      }
    },
    clearOpenedSubmenu(state) {
      state.openedSubmenuArr.length = 0;
    },
    changeMenuTheme(state, theme) {
      state.menuTheme = theme;
    },
    changeMainTheme(state, mainTheme) {
      state.theme = mainTheme;
    },
    setMenuList(state, menuList) {
      state.menuList = menuList;
    },
    updateMenuList(state) {
      let appRouters = [];
      let len = state.originRouters.length;
      for (let r = 0; r < len; r++) {
        appRouters.push(state.originRouters[r])
      }
      let accesses = Vue.ls.get('access').split(',');
      let codes = [];
      accesses.forEach((item) => {
        codes.push(Number(item))
      });
      let accessCodes = codes;
      let menuList = [];
      let menuLen = menuList.length;
      for (let i = 0; i < appRouters.length; i++) {
        for (let k = 0; k < appRouters[i].children.length; k++) {
          if (accessCodes.indexOf(appRouters[i].children[k].access) < 0) {
            appRouters[i].children.splice(k, 1);
            k--;
          }
        }
        menuList.push(appRouters[i])
      }
      if (menuList.length > 0) {
        for (let s = 0; s < menuLen; s++) {
          if (menuList[s].children.length === 0) {
            menuList.splice(s, 1);
            s--;
          }
        }
        state.menuList = menuList
      }
    },
    setAvator(state, path) {
      localStorage.avatorImgPath = path;
    },
    clearAllTags (state) {
      state.pageOpenedList.splice(1);
      state.cachePage.length = 0;
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    clearOtherTags (state, vm) {
      let currentName = vm.$route.name;
      let currentIndex = 0;
      state.pageOpenedList.forEach((item, index) => {
        if (item.name === currentName) {
          currentIndex = index;
        }
      });
      if (currentIndex === 0) {
        state.pageOpenedList.splice(1);
      } else {
        state.pageOpenedList.splice(currentIndex + 1);
        state.pageOpenedList.splice(1, currentIndex - 1);
      }
      let newCachepage = state.cachePage.filter(item => {
        return item === currentName;
      });
      state.cachePage = newCachepage;
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
  },
  actions: {}
};
export default app;
